package com.megvii.koala.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.corelib.util.IMEUtil;
import com.megvii.koala.R;
import com.megvii.koala.activity.params.ChangeServerParam;
import com.megvii.koala.base.KoalaActivity;
import com.megvii.koala.components.TitleView;
import com.megvii.koala.presenter.ChangeServerPresenter;

/**
 * Created by linyue on 16/2/29.
 */
public class ChangeServerActivity extends KoalaActivity<ChangeServerParam>
        implements TitleView.TitleListener, View.OnClickListener {
    protected TitleView titleView;

    protected View onlineContainer;

    protected TextView onlineAddress;

    protected ImageView onlineFlag;

    protected View offlineContainer;

    protected EditText offlineAddress;

    protected ImageView offlineFlag;

    private boolean stateOnline = false;

    private ChangeServerListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = R.layout.changeserver_layout;
        if (activityParam != null) {
            layoutId = activityParam.layoutId;
        }
        setContentView(layoutId);
        titleView = (TitleView) findViewById(R.id.changeserver_layout_title);
        onlineContainer = findViewById(R.id.changeserver_layout_onlineContainer);
        onlineAddress = (TextView) findViewById(R.id.changeserver_layout_onlineAddress);
        onlineFlag = (ImageView) findViewById(R.id.changeserver_layout_onlineFlag);
        offlineContainer = findViewById(R.id.changeserver_layout_offlineContainer);
        offlineAddress = (EditText) findViewById(R.id.changeserver_layout_offlineAddress);
        offlineAddress.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    EditText et_offline = (EditText) v;
                    if (et_offline.getText().toString().equals("")) {
                        et_offline.setText("http://");
                        et_offline.setSelection(et_offline.getText().length());
                    }
                }
            }
        });
        offlineFlag = (ImageView) findViewById(R.id.changeserver_layout_offlineFlag);
        titleView.setTitleListener(this);
        onlineContainer.setOnClickListener(this);
        offlineAddress.setOnClickListener(this);
    }

    public String getOnlineAddress() {
        return onlineAddress.getText().toString();
    }

    public void setOnlineAddress(String text) {
        onlineAddress.setText(text);
    }

    public String getOfflineAddress() {
        return offlineAddress.getText().toString();
    }

    public void setOfflineAddress(String text) {
        offlineAddress.setText(text);
    }

    public boolean isStateOnline() {
        return stateOnline;
    }

    /**
     * 设置是选择了在线还是离线，用于选择后面的对勾显示在哪里
     */
    public void setStateOnline(boolean online) {
        stateOnline = online;
        if (online) {
            onlineFlag.setVisibility(View.VISIBLE);
            offlineFlag.setVisibility(View.GONE);
            offlineAddress.clearFocus();
            offlineAddress.setFocusable(false);
            IMEUtil.HideIME(this, offlineAddress);
        } else {
            onlineFlag.setVisibility(View.GONE);
            offlineFlag.setVisibility(View.VISIBLE);
            offlineAddress.setFocusable(true);
            offlineAddress.setFocusableInTouchMode(true);
            offlineAddress.requestFocus();
            IMEUtil.ShowIME(this, offlineAddress);
        }
    }

    @Override
    public void onClickReturn() {
        if (listener != null) {
            listener.onClickReturn();
        }
    }

    @Override
    public void onClickFunction() {
        if (listener != null) {
            listener.onClickSave();
        }
    }

    @Override
    public Class getPresenterClass() {
        return ChangeServerPresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView() {
        return null;
    }

    public void setListener(ChangeServerListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            int id = v.getId();
            if (id == R.id.changeserver_layout_onlineContainer) {
                listener.onClickOnline();
            } else if (id == R.id.changeserver_layout_offlineAddress) {
                listener.onClickOffline();
            }
        }
    }

    public interface ChangeServerListener {
        public void onClickReturn();

        public void onClickSave();

        public void onClickOnline();

        public void onClickOffline();
    }
}
